/*-------------------------------------------------------------------------
 *
 *	  EUC_KR 和 MULE_INTERNAL
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/euc_kr_and_mic/euc_kr_and_mic.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(euc_kr_to_mic);
PG_FUNCTION_INFO_V1(mic_to_euc_kr);

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */

static int	fc_euc_kr2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_mic2euc_kr(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);

Datum euc_kr_to_mic(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_KR, PG_MULE_INTERNAL);

	fc_converted = fc_euc_kr2mic(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum mic_to_euc_kr(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_KR);

	fc_converted = fc_mic2euc_kr(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

/*
 * EUC_KR ---> MIC
 */
static int fc_euc_kr2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_euc;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_euc;
		if (IS_HIGHBIT_SET(fc_c1))
		{
			fc_l = pg_encoding_verifymbchar(PG_EUC_KR, (const char *) fc_euc, fc_len);
			if (fc_l != 2)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_KR,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = LC_KS5601;
			*fc_p++ = fc_c1;
			*fc_p++ = fc_euc[1];
			fc_euc += 2;
			fc_len -= 2;
		}
		else
		{						/* 应该是 ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_KR,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_euc++;
			fc_len--;
		}
	}
	*fc_p = '\0';

	return fc_euc - fc_start;
}

/*
 * MIC ---> EUC_KR
 */
static int fc_mic2euc_kr(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_mic;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_mic;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_MULE_INTERNAL,
										(const char *) fc_mic, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_mic++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) fc_mic, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_MULE_INTERNAL,
									(const char *) fc_mic, fc_len);
		}
		if (fc_c1 == LC_KS5601)
		{
			*fc_p++ = fc_mic[1];
			*fc_p++ = fc_mic[2];
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_MULE_INTERNAL, PG_EUC_KR,
									   (const char *) fc_mic, fc_len);
		}
		fc_mic += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_mic - fc_start;
}
